package com.xiang.springboot.demo.httpsession.module1.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/module1")
public class TestController {
    @GetMapping("/page1")
    public String page1(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attr, ModelMap map) {
        System.out.println(attr.getFlashAttributes().get("test"));
        System.out.println("aaaaaaaaaaaaaa" + map.get("test"));
        HttpSession session = request.getSession();
        session.setAttribute("sessiontest", "session test text...." + session.getId());
        String url = request.getScheme() + ":111//" + request.getServerName() + ":" + request.getServerPort() + "-" + request.getLocalPort() + "/" + request.getContextPath() + "/" + request.getRequestURI();
        session.setAttribute("ctpath", url);
        return "module1/page1";
    }

    @GetMapping("/page2")
    public String page2(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attr) {
        attr.addFlashAttribute("test", "testaaaaa");
        return "redirect:/module1/page1";
    }

    @GetMapping("/sm")
    @ResponseBody
    public String sessionRemove(HttpServletRequest request, HttpServletResponse response, RedirectAttributes attr) {
        request.getSession().invalidate();
        return "ok";
    }
}
